package com.yuren.codecrushlearn.Q5200;

import java.util.Arrays;

/**
 * Created with Intellij IDEA.
 * Description:
 * 基于开放寻址法实现HashSet
 * @author 10204
 * @date 2025-04-21 23:57
 */
public class Q5213 {
    private static final int SetSize = 256;
    private final int[] table = new int[SetSize];

    public Q5213() {
        Arrays.fill(table, -1);
    }

    private int hash(int key) {
        return key % SetSize;
    }

    public void add(int key) {
        int idx = hash(key);
        while (true) {
            if (table[idx] == -1) {
                table[idx] = key;
                return;
            } else if (table[idx] == key) {
                return;
            }
            idx = hash(idx + 1);
        }
    }

    public boolean contains(int key) {
        int idx = hash(key);
        while (true) {
            if (table[idx] == key) {
                return true;
            } else if (table[idx] == -1) {
                return false;
            }
            idx = hash(idx + 1);
        }
    }
}
